function WriteTrajectories_c3max(Traj,ObjectsContainedInFrames,textFileName,OutputDir,parameters,lengthOfTrajToSave,propsToOutput)

TN=parameters.TN;
ON=parameters.ON;
numOfColors=parameters.numcolors;
step=parameters.step;

fidO=fopen([OutputDir textFileName],'w');

h = waitbar(0,'Writing trajectories file');




num_of_traj=length(Traj);
traj_indices=1:num_of_traj;

l=zeros(num_of_traj,1);
for i=1:length(Traj)
    [l(i) temp]=size(Traj{i});
end

lengthOfLongestTraj=max(l);

traj_indices_toSave=traj_indices(l>lengthOfTrajToSave);

%Setup the tab string, which is inserted if blank output fields are
%necessary

totalOutParam=0;
outString{1}=[];

if(propsToOutput.Frame)
    totalOutParam=totalOutParam+1;
    outString{totalOutParam}=sprintf('%s\t','Frame');
end
if(propsToOutput.Area)
   totalOutParam=totalOutParam+1;
   outString{totalOutParam}=sprintf('%s\t','Area');
end
if(propsToOutput.Centroid)
   totalOutParam=totalOutParam+2;
   outString{totalOutParam-1}=sprintf('%s\t','Centroid X');
   outString{totalOutParam}=sprintf('%s\t','Centroid Y');   
end
if(propsToOutput.Eccentricity)
  totalOutParam=totalOutParam+1;
  outString{totalOutParam}=sprintf('%s\t','Eccentricity');
end
if(propsToOutput.Orientation)
   totalOutParam=totalOutParam+1;
   outString{totalOutParam}=sprintf('%s\t','Orientation');
end
if(propsToOutput.fluorMean)
   for j=1:numOfColors
        outString{totalOutParam+j}=sprintf('%s\t',['c' num2str(j+1) ' mean']);
   end
   totalOutParam=totalOutParam+numOfColors; 
end
if(propsToOutput.fluorStd)
   for j=1:numOfColors
        outString{totalOutParam+j}=sprintf('%s\t',['c' num2str(j+1) ' std']);
   end
   totalOutParam=totalOutParam+numOfColors;
end
if(propsToOutput.c2c3ratio)
   totalOutParam=totalOutParam+1;
   outString{totalOutParam}=sprintf('%s\t','c2/c3 ratio');
end
%setup to only output c2 max
if(propsToOutput.fluorMax)
   %for i=1:numOfColors
   totalOutParam=totalOutParam+1;
   outString{totalOutParam}=sprintf('%s\t','c2 max');
   %end
end
%setup to only output c2 PrcRatio
if(propsToOutput.fluorPrcRatio)
   %for i=1:numOfColors
   totalOutParam=totalOutParam+1;
   outString{totalOutParam}=sprintf('%s\t','c2 PrcRatio');
   %end
end

% add c3 max
if(propsToOutput.fluorMaxC3)
    totalOutParam=totalOutParam+1;
    outString{totalOutParam}=sprintf('%s\t','c3 max');
end
   
tabString=cell(1,totalOutParam);
for i=1:totalOutParam
    tabString{i}='\t';
end

for i=1:length(traj_indices_toSave)
% prints the trajectory number for each desired trajectory
    fprintf(fidO,'%d',traj_indices_toSave(i));
    fprintf(fidO,[tabString{:}]);  
end
fprintf(fidO,'\n');
for i=1:length(traj_indices_toSave)
    fprintf(fidO,'%s',[outString{:}]);
end
fprintf(fidO,'\n');

if(propsToOutput.unshifted)
    for j=1:lengthOfLongestTraj
        for i=1:length(traj_indices_toSave)
            if(length(Traj{traj_indices_toSave(i)})>=j)
                frame=Traj{traj_indices_toSave(i)}(j,TN.frame);
                object=Traj{traj_indices_toSave(i)}(j,TN.object);
                
                dataString = formatDataString_c3max(propsToOutput, ObjectsContainedInFrames,ON, frame, object, step, numOfColors);
                fprintf(fidO,'%s',[dataString{:}]);
            else
                fprintf(fidO,[tabString{:}]);
            end
        end
        fprintf(fidO,'\n');
        waitbar(j / lengthOfLongestTraj,h)
    end
end
if(propsToOutput.shifted)
    for j=1:length(ObjectsContainedInFrames)
        for i=1:length(traj_indices_toSave)
            if(any(j==Traj{traj_indices_toSave(i)}(:,TN.frame)))
                frame=j;
                object=Traj{traj_indices_toSave(i)}(j==Traj{traj_indices_toSave(i)}(:,TN.frame),TN.object);
                dataString = formatDataString_c3max(propsToOutput, ObjectsContainedInFrames,ON, frame, object, step, numOfColors);
                fprintf(fidO,'%s',[dataString{:}]);
            else
                fprintf(fidO,[tabString{:}]);
            end
        end
        fprintf(fidO,'\n');
        waitbar(j / length(ObjectsContainedInFrames),h)
    end
end
fprintf(fidO,'\n');

fclose(fidO);
'wrote traj props file'

function dataString = formatDataString_c3max(propsToOutput, ObjectsContainedInFrames, ON,frame, object, step, numOfColors)
totalOutParam=0;

if(propsToOutput.Frame)
  totalOutParam=totalOutParam+1;
  dataString{totalOutParam}=sprintf('%f\t',frame);
end
if(propsToOutput.Area)
    totalOutParam=totalOutParam+1;
    dataString{totalOutParam}=sprintf('%f\t',ObjectsContainedInFrames{frame}(object,ON.Area));
end
if(propsToOutput.Centroid)
    totalOutParam=totalOutParam+1;
    Centroid(1)=ObjectsContainedInFrames{frame}(object,ON.CentroidX);
    Centroid(2)=ObjectsContainedInFrames{frame}(object,ON.CentroidY);
    dataString{totalOutParam}=sprintf('%f\t',Centroid);
end
if(propsToOutput.Eccentricity)
    totalOutParam=totalOutParam+1;
    dataString{totalOutParam}=sprintf('%f\t',ObjectsContainedInFrames{frame}(object,ON.Eccentricity));
end
if(propsToOutput.Orientation)
    totalOutParam=totalOutParam+1;
    dataString{totalOutParam}=sprintf('%f\t',ObjectsContainedInFrames{frame}(object,ON.Orientation));
end
if(propsToOutput.fluorMean)
    if(mod(frame-1,step) == 0)
        totalOutParam=totalOutParam+1;
        dataString{totalOutParam}=sprintf('%f\t',ObjectsContainedInFrames{frame}(object,ON.fluorMean));
    else
        for k=1:numOfColors
            dataString{totalOutParam+k}=sprintf('\t');
        end
        totalOutParam=totalOutParam+numOfColors;
    end
end
if(propsToOutput.fluorStd)
    
    if(mod(frame-1,step) == 0)
        totalOutParam=totalOutParam+1;
        dataString{totalOutParam}=sprintf('%f\t',ObjectsContainedInFrames{frame}(object,ON.fluorStd));
    else
        for k=1:numOfColors
            dataString{totalOutParam+k}=sprintf('\t');
        end
        totalOutParam=totalOutParam+numOfColors;
    end
end
if(propsToOutput.c2c3ratio)
    totalOutParam=totalOutParam+1;
    if(mod(frame-1,step) == 0)       
        dataString{totalOutParam}=sprintf('%f\t',ObjectsContainedInFrames{frame}(object,ON.fluorMean(1))/...
            ObjectsContainedInFrames{frame}(object,ON.fluorMean(2)));
    else
        dataString{totalOutParam}=sprintf('\t');
    end
end
%output c2 fluor Max only - in later release expand to all
%channels
if(propsToOutput.fluorMax)
    totalOutParam=totalOutParam+1;
    if(mod(frame-1,step) == 0)
        dataString{totalOutParam}=sprintf('%f\t',ObjectsContainedInFrames{frame}(object,ON.fluorMax(1)));
    else
        dataString{totalOutParam}=sprintf('\t');
    end
end
if(propsToOutput.fluorPrcRatio)
    totalOutParam=totalOutParam+1;
    if(mod(frame-1,step) == 0)
        dataString{totalOutParam}=sprintf('%f\t',ObjectsContainedInFrames{frame}(object,ON.fluorPrcRatio(1)));
    else
        dataString{totalOutParam}=sprintf('\t');
    end
end

% add c3max to the output list
if(propsToOutput.fluorMaxC3)
    totalOutParam=totalOutParam+1;
    if(mod(frame-1,step) == 0)
        dataString{totalOutParam}=sprintf('%f\t',ObjectsContainedInFrames{frame}(object,ON.fluorMax(2)));
    else
        dataString{totalOutParam}=sprintf('\t');
    end
end


            
            
  
